package com.zlz.open.ugs.module;

import cn.hutool.core.collection.CollectionUtil;
import com.zlz.open.ugs.common.config.enums.ModuleType;
import com.zlz.open.ugs.core.chain.Chain;
import com.zlz.open.ugs.core.exchange.Exchange;
import com.zlz.open.ugs.core.module.Module;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * 鉴权组件
 *
 * @Author: zhulinzhong
 * @Date: 2024/5/30
 */
@Slf4j
@Component
public class OAuthModule implements Module {

    public Mono<Void> work(Exchange exchange, Chain chain) {
        HttpHeaders headers = exchange.webExchange().getRequest().getHeaders();
        List<String> authorization = headers.get("Authorization");
        if (CollectionUtil.isEmpty(authorization)) {
            return exchange.response().write(HttpStatus.UNAUTHORIZED);
        }
        log.info("OAuthModule work");
        return chain.work(exchange);
    }

}
